{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "全连接cpu.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yejianfeng2014/AI/blob/master/%E5%85%A8%E8%BF%9E%E6%8E%A5cpu.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "metadata": {
        "id": "zx8BKEFjJBGf",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 667
        },
        "outputId": "201b49e0-6cc1-4308-c19d-66fb256fff2b"
      },
      "cell_type": "code",
      "source": [
        "import tensorflow as tf\n",
        "\n",
        "from  tensorflow.examples.tutorials.mnist import input_data\n",
        "\n",
        "# 1.设置输入和输出节点的个数,配置神经网络的参数。\n",
        "INPUT_NODE = 784  # 输入节点\n",
        "OUTPUT_NODE = 10  # 输出节点\n",
        "LAYER1_NODE = 500  # 隐藏层数\n",
        "\n",
        "BATCH_SIZE = 100  # 每次batch打包的样本个数\n",
        "\n",
        "# 模型相关的参数\n",
        "LEARNING_RATE_BASE = 0.8\n",
        "LEARNING_RATE_DECAY = 0.99\n",
        "REGULARAZTION_RATE = 0.0001\n",
        "TRAINING_STEPS = 5000\n",
        "MOVING_AVERAGE_DECAY = 0.99\n",
        "\n",
        "\n",
        "# 2. 定义辅助函数来计算前向传播结果，使用ReLU做为激活函数。\n",
        "\n",
        "def inference(input_tensor, avg_class, weights1, biases1, weights2, biases2):\n",
        "    # 不使用滑动平均类\n",
        "    if avg_class == None:\n",
        "        layer1 = tf.nn.relu(tf.matmul(input_tensor, weights1) + biases1)\n",
        "        return tf.matmul(layer1, weights2) + biases2\n",
        "\n",
        "    else:\n",
        "        # 使用滑动平均类\n",
        "        layer1 = tf.nn.relu(tf.matmul(input_tensor, avg_class.average(weights1)) + avg_class.average(biases1))\n",
        "        return tf.matmul(layer1, avg_class.average(weights2)) + avg_class.average(biases2)\n",
        "\n",
        "\n",
        "# 3. 定义训练过程。\n",
        "\n",
        "def train(mnist):\n",
        "    x = tf.placeholder(tf.float32, [None, INPUT_NODE], name='x-input')\n",
        "    y_ = tf.placeholder(tf.float32, [None, OUTPUT_NODE], name='y-input')\n",
        "    # 生成隐藏层的参数。\n",
        "    weights1 = tf.Variable(tf.truncated_normal([INPUT_NODE, LAYER1_NODE], stddev=0.1))\n",
        "    biases1 = tf.Variable(tf.constant(0.1, shape=[LAYER1_NODE]))\n",
        "    # 生成输出层的参数。\n",
        "    weights2 = tf.Variable(tf.truncated_normal([LAYER1_NODE, OUTPUT_NODE], stddev=0.1))\n",
        "    biases2 = tf.Variable(tf.constant(0.1, shape=[OUTPUT_NODE]))\n",
        "\n",
        "    # 计算不含滑动平均类的前向传播结果\n",
        "    y = inference(x, None, weights1, biases1, weights2, biases2)\n",
        "\n",
        "    # 定义训练轮数及相关的滑动平均类\n",
        "    global_step = tf.Variable(0, trainable=False)\n",
        "    variable_averages = tf.train.ExponentialMovingAverage(MOVING_AVERAGE_DECAY, global_step)\n",
        "    variables_averages_op = variable_averages.apply(tf.trainable_variables())\n",
        "    average_y = inference(x, variable_averages, weights1, biases1, weights2, biases2)\n",
        "\n",
        "    # 计算交叉熵及其平均值\n",
        "    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=y, labels=tf.argmax(y_, 1))\n",
        "    cross_entropy_mean = tf.reduce_mean(cross_entropy)\n",
        "\n",
        "    # 损失函数的计算\n",
        "    regularizer = tf.contrib.layers.l2_regularizer(REGULARAZTION_RATE)\n",
        "\n",
        "    regularaztion = regularizer(weights1) + regularizer(weights2)\n",
        "\n",
        "    loss = cross_entropy_mean + regularaztion\n",
        "\n",
        "    # 设置指数衰减的学习率。\n",
        "    learning_rate = tf.train.exponential_decay(\n",
        "        LEARNING_RATE_BASE,\n",
        "        global_step,\n",
        "        mnist.train.num_examples / BATCH_SIZE,\n",
        "        LEARNING_RATE_DECAY,\n",
        "        staircase=True)\n",
        "\n",
        "    # 优化损失函数\n",
        "    train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss, global_step=global_step)\n",
        "\n",
        "    # 反向传播更新参数和更新每一个参数的滑动平均值\n",
        "    with tf.control_dependencies([train_step, variables_averages_op]):\n",
        "        train_op = tf.no_op(name='train')\n",
        "\n",
        "    # 计算正确率\n",
        "    correct_prediction = tf.equal(tf.argmax(average_y, 1), tf.argmax(y_, 1))\n",
        "    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
        "\n",
        "    # 初始化会话，并开始训练过程。\n",
        "    with tf.Session() as sess:\n",
        "        tf.global_variables_initializer().run()\n",
        "        validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels}\n",
        "        test_feed = {x: mnist.test.images, y_: mnist.test.labels}\n",
        "\n",
        "        # 循环的训练神经网络。\n",
        "        for i in range(TRAINING_STEPS):\n",
        "            if i % 1000 == 0:\n",
        "                validate_acc = sess.run(accuracy, feed_dict=validate_feed)\n",
        "                print(\"After %d training step(s), validation accuracy using average model is %g \" % (i, validate_acc))\n",
        "\n",
        "            xs, ys = mnist.train.next_batch(BATCH_SIZE)\n",
        "            sess.run(train_op, feed_dict={x: xs, y_: ys})\n",
        "\n",
        "        test_acc = sess.run(accuracy, feed_dict=test_feed)\n",
        "        print((\"After %d training step(s), test accuracy using average model is %g\" % (TRAINING_STEPS, test_acc)))\n",
        "\n",
        "\n",
        "def main(argv=None):\n",
        "    mnist = input_data.read_data_sets(\"../../../data/MNIST_data\", one_hot=True)\n",
        "    train(mnist)\n",
        "\n",
        "\n",
        "if __name__ == '__main__':\n",
        "    main()\n",
        "\n",
        "\n",
        "# After 5000 training step(s), test accuracy using average model is 0.9836"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "WARNING:tensorflow:From <ipython-input-1-eabd03be70d5>:105: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
            "Instructions for updating:\n",
            "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
            "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
            "Instructions for updating:\n",
            "Please write your own downloading logic.\n",
            "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/learn/python/learn/datasets/base.py:252: _internal_retry.<locals>.wrap.<locals>.wrapped_fn (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
            "Instructions for updating:\n",
            "Please use urllib or similar directly.\n",
            "Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.\n",
            "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
            "Instructions for updating:\n",
            "Please use tf.data to implement this functionality.\n",
            "Extracting ../../../data/MNIST_data/train-images-idx3-ubyte.gz\n",
            "Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.\n",
            "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
            "Instructions for updating:\n",
            "Please use tf.data to implement this functionality.\n",
            "Extracting ../../../data/MNIST_data/train-labels-idx1-ubyte.gz\n",
            "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
            "Instructions for updating:\n",
            "Please use tf.one_hot on tensors.\n",
            "Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.\n",
            "Extracting ../../../data/MNIST_data/t10k-images-idx3-ubyte.gz\n",
            "Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.\n",
            "Extracting ../../../data/MNIST_data/t10k-labels-idx1-ubyte.gz\n",
            "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
            "Instructions for updating:\n",
            "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
            "After 0 training step(s), validation accuracy using average model is 0.0714 \n",
            "After 1000 training step(s), validation accuracy using average model is 0.978 \n",
            "After 2000 training step(s), validation accuracy using average model is 0.9822 \n",
            "After 3000 training step(s), validation accuracy using average model is 0.9846 \n",
            "After 4000 training step(s), validation accuracy using average model is 0.9842 \n",
            "After 5000 training step(s), test accuracy using average model is 0.9822\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "iOqXRQ_SJIIN",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}